package com.team.ssm_item_lj.controller;

import com.team.ssm_item_lj.entity.Users;
import com.team.ssm_item_lj.service.UsersService;
import com.team.ssm_item_lj.service.impl.UsersServiceImpl;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @Description
 * @Author teacher_zhu
 * @Date 2024/12/5
 * @Version 1.2.3
 */
@Controller //可以返回页面
public class LoginController {

    @Autowired //自动注入
    UsersService usersService;

    @RequestMapping("/login")
   public  String login(String username, String password, HttpSession session){

       System.out.println("验证用户通过～～～～～～～～～");

        System.out.println("username = " + username);
        System.out.println("password =" + password);
        //验证用户是否存在
         Users users = usersService.login(username, password);
        System.out.println("登录后的用户信息users = " + users.toString());
        //保存用户登录信息到session
        if(users != null){
            session.setAttribute("userLogin", users);
            System.out.println("用户登录成功～～～～～～～～～");
            return "index";
        }else{
            System.out.println("用户不存在～～～～～～～～～");
            return "login";
        }

   }


    @RequestMapping("/loginOut")
    public  String loginOut(HttpSession session){

        System.out.println("用户退出～～～～～～～～～");
        //从会话中删除用户的信息
        session.removeAttribute("userLogin"); //释放用信息


        return "login";
    }






}
